import { _decorator, Component, Node, sp } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
    private skeleton: sp.Skeleton = null!;
    private bone = null!;
    private initialRotation: number = 0;
    private readonly MAX_ROTATION_ANGLE: number = Math.PI / 2;

    protected onLoad(): void {
        this.skeleton = this.node.getComponentInChildren(sp.Skeleton);
        this.bone = this.skeleton.findBone("kongzhi");
        if (this.bone) {
            this.initialRotation = this.bone.rotation;
        }
        this.playAnim('animation2', true);
    }

    start() {

    }

    update(deltaTime: number) {

    }

    public playAnim(animName: string, loop: boolean) {
        this.skeleton.setAnimation(0, animName, loop);
        this.skeleton.setCompleteListener(() => {
            this.playAnim('animation2', true);
        });
    }

    public rotateBone(angle: number) {
        if (this.bone) {
            const clampedAngle = Math.max(-this.MAX_ROTATION_ANGLE, Math.min(this.MAX_ROTATION_ANGLE, angle));
            this.bone.rotation = clampedAngle * (180 / Math.PI) + 90;
        }
    }

    public resetBoneRotation() {
        if (this.bone) {
            this.bone.rotation = this.initialRotation;
        }
    }
}


